package com.lzw.okgoutils.sample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.lzw.okgoutils.R;
import com.lzw.okgoutils.http.HttpCallback;
import com.lzw.okgoutils.http.RequestParams;
import com.lzw.okgoutils.http.otherutils.BaseConstant;
import com.lzw.okgoutils.http.otherutils.LogUtil;
import com.lzw.okgoutils.http.struct.StringHashMap;

import java.util.ArrayList;
import java.util.List;

import static com.lzw.okgoutils.http.otherutils.BaseConstant.ACTION_LOGIN_IN;
import static com.lzw.okgoutils.http.otherutils.BaseConstant.ACTION_LOGIN_OUT;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, HttpCallback {

    private static final String TAG = MainActivity.class.getSimpleName();
    private Button mButton;
    private TextView mTextView;
    //把tag存在list集合里面
    private List<Integer> mTagList = null;
    private static final int ACTIONS_LOGIN_IN = 0;
    private static final int ACTIONS_LOGIN_OUT = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTagList = new ArrayList<Integer>();
        initView();
        initListener();
    }

    private void initListener() {
        mButton.setOnClickListener(this);
    }

    private void initView() {
        mButton = (Button) findViewById(R.id.btn);
        mTextView = (TextView) findViewById(R.id.tv);
    }

    @Override
    public void onClick(View v) {
        showProgressDialog();
        //调用登录接口
        doRequest(ACTIONS_LOGIN_IN);
    }

    private void doRequest(int action) {
        RequestParams params = new RequestParams();
        StringHashMap map = new StringHashMap();
        map.put("name", "userName");
        map.put("pwd", "password");
        params.setAllStrParams(map);
        String result = getWhichAction(action);
        int tag = 0;
        if (result.equals(ACTION_LOGIN_IN)) {
            tag = action;
            LoginAction.getInstance().loginIn(getWhichAction(ACTIONS_LOGIN_IN), params, this, tag);
        } else if (result.equals(ACTION_LOGIN_OUT)) {
            tag = action;
            LoginAction.getInstance().loginIn(getWhichAction(ACTIONS_LOGIN_OUT), params, this, tag);
        }
        if (mTagList != null && !mTagList.contains(tag)) {
            mTagList.add(tag);
        }
    }

    public String getWhichAction(int action) {
        if (action == ACTIONS_LOGIN_IN) {
            return BaseConstant.getActionName(ACTION_LOGIN_IN);
        } else if (action == ACTIONS_LOGIN_OUT) {
            return BaseConstant.getActionName(ACTION_LOGIN_OUT);
        }
        return null;
    }

    //显示进度条
    private void showProgressDialog() {
        //具体逻辑自己去写，这里就不多说了
    }

    //隐藏进度条
    private void hideProgressDialog() {
        //具体逻辑自己去写，这里就不多说了
    }

    //把数据设置给View
    private void setDataToView(Object object) {
        //把object转成Datas实体类对象，设置给TextView
        //Datas data = (Datas)object;
        //mTextView.setText(data.getXXX());
    }

    //网络请求成功的回调
    @Override
    public void onSuccess(String result, Object tag) {
        try {
            if (!TextUtils.isEmpty(result)) {
                hideProgressDialog();
                //如果有不同的接口，返回不同的实体类对象，可以通过tag去区分是哪个接口
                //实体类，这里就不写了，只讲解大概的逻辑
                if (tag == ACTIONS_LOGIN_IN) {
                   /* Datas data = null;
                    data = new GsonBuilder().setDateFormat("yyyy-MM-dd")
                            .create().fromJson(result, Datas.class);
                    if (data != null) {
                        setDataToView(data);*/
                } else if (tag == ACTIONS_LOGIN_OUT) {
                    //Datas是实体类
                     /* Datas2 data2 = null;
                     data2 = new GsonBuilder().setDateFormat("yyyy-MM-dd")
                        .create().fromJson(result, Datas2.class);
                     if (data2 != null) {
                        setDataToView(data2);
                     }*/
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.e(TAG, "onSuccess = " + e.toString());
        }
    }

    //网络请求失败的回调
    @Override
    public void onError(String result, Object tag) {
        showError();
    }

    private void showError() {
        Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
    }


}
